function longestCommonSubsequence(arr1, arr2) {
  let i = 0;
  let j = 0;
  let lcs = [];

  while (i < arr1.length && j < arr2.length) {
    if (arr1[i] === arr2[j]) {
      lcs.push(arr1[i]);
      i++;
      j++;
    } else if (arr1[i] < arr2[j]) {
      i++;
    } else {
      j++;
    }
  }

  return lcs;
}

// 示例用法
console.log(longestCommonSubsequence([1, 3, 4, 5, 6, 7], [3, 5, 7, 8])); // 输出: [3, 5, 7]
